package gui;

import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.AppException;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.Contact;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.ContactManager;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.ContactManagerImpl;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.ContactsManagerImpl;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.Group;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.GroupManager;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.GroupManagerImpl;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.GroupType;
import cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.NumberType;
import java.io.FileInputStream;
import java.util.List;
import java.util.Locale;

import java.util.Map;
//import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
//import javax.swing.*;
import org.apache.commons.dbcp.BasicDataSource;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author lencii
 */
public class ContactsManager extends javax.swing.JFrame {
    
    Locale locale_cs = new Locale("cs");
    Locale locale_en = new Locale("en");
    Locale locale_sk = new Locale("sk");
    
       

    
    ResourceBundle translate=ResourceBundle.getBundle("Bundle");//, locale_cs);//Locale.getDefault());
    
    BasicDataSource basicDataSource = new BasicDataSource();
    private static final Logger log = Logger.getLogger(ContactsManager.class.getName());
    
    ContactManager contactManager;
    GroupManager groupManager;
    cz.muni.fi.pv168.projekt.pv168_semestralny_projekt.ContactsManager contactsManager;
    
    ContactTableModel contactTableModel;
    NumberTableModel numberTableModel;
    
    
    
    private ContactsSwingWorker contactsSwingWorker;
    private class ContactsSwingWorker extends SwingWorker<Void, Contact> {

	@Override
	protected Void doInBackground() throws Exception {
            contactTableModel = (ContactTableModel) jTableContact.getModel();
            contactTableModel.setContactManager(contactManager);
	    for (Contact contact : contactManager.findAllContacts()) {
		publish(contact);
	    }
	    return null;
	}
	
	@Override
	protected void process(List<Contact> items) {
	    for (Contact i : items) {
                contactTableModel.addContact(i);
	    }
	}

        @Override
        protected void done() {
            contactsSwingWorker = null;
        }
    }
    
    private void setUp() throws Exception {
        Properties configFile = new Properties();
        configFile.load(new FileInputStream("src/main/resources/config.properties"));
        log.log(Level.INFO, "loaded configfile");
	BasicDataSource bds = new BasicDataSource();
	bds.setUrl( configFile.getProperty( "url" ) );
	bds.setPassword( configFile.getProperty( "password" ) );
	bds.setUsername( configFile.getProperty( "username" ) );
	basicDataSource = bds;
    }
    
    /**
     * Creates new form ContactsManager
     */
    public ContactsManager() {
        log.log(Level.INFO, "Starting application");
        Locale.setDefault(locale_cs);
        translate = ResourceBundle.getBundle("Bundle");
        try {
            setUp();
        } catch (Exception ex) {
	    String msg = "Application setup failed.";
            log.log(Level.SEVERE, msg, ex);
        }
        Locale defaultLocale =Locale.getDefault();

        
        initComponents();
        
        contactManager	= new ContactManagerImpl(basicDataSource);
        groupManager    = new GroupManagerImpl(basicDataSource);
        contactsManager = new ContactsManagerImpl(basicDataSource);
        
        numberTableModel = (NumberTableModel) jTableNumber.getModel();
        numberTableModel.setContactManager(contactManager);
        
        contactsSwingWorker = new ContactsSwingWorker();
        contactsSwingWorker.execute();
        
        textContactID.setVisible(false);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialogGroup = new javax.swing.JDialog();
        jLabelNewEditGroup = new javax.swing.JLabel();
        jLabelGroupType = new javax.swing.JLabel();
        jComboBoxGroupType = new javax.swing.JComboBox();
        jLabelGroupNote = new javax.swing.JLabel();
        jTextFieldGroupNote = new javax.swing.JTextField();
        jButtonGroupCancel = new javax.swing.JButton();
        jButtonGroupOK = new javax.swing.JButton();
        jDialogDeleteGroup = new javax.swing.JDialog();
        jLabelDeleteGroup = new javax.swing.JLabel();
        jLabelDeleteGroupType = new javax.swing.JLabel();
        jComboBoxDeleteGroup = new javax.swing.JComboBox();
        jButtonDeleteGroupOK = new javax.swing.JButton();
        jButtonDeleteGroupCancel = new javax.swing.JButton();
        jDialogContact = new javax.swing.JDialog();
        jLabelEditContact = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabelSurname = new javax.swing.JLabel();
        jLabelAddress = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jTextFieldSurname = new javax.swing.JTextField();
        jTextFieldAddress = new javax.swing.JTextField();
        jPanelAddToGroup = new javax.swing.JPanel();
        jLabelAddToGroup = new javax.swing.JLabel();
        jComboBoxContactGroup = new javax.swing.JComboBox();
        jLabelNumbers = new javax.swing.JLabel();
        jPanelNumbers = new javax.swing.JPanel();
        jComboBoxNumbers1 = new javax.swing.JComboBox();
        jComboBoxNumbers2 = new javax.swing.JComboBox();
        jComboBoxNumbers3 = new javax.swing.JComboBox();
        jComboBoxNumbers4 = new javax.swing.JComboBox();
        jComboBoxNumbers5 = new javax.swing.JComboBox();
        jTextFieldNumbers1 = new javax.swing.JTextField();
        jTextFieldNumbers2 = new javax.swing.JTextField();
        jTextFieldNumbers3 = new javax.swing.JTextField();
        jTextFieldNumbers4 = new javax.swing.JTextField();
        jTextFieldNumbers5 = new javax.swing.JTextField();
        jButtonContactOK = new javax.swing.JButton();
        jButtonContactCancel = new javax.swing.JButton();
        textContactID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jShowGroup = new javax.swing.JComboBox();
        jNote = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableContact = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableNumber = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabelContact = new javax.swing.JLabel();
        jButtonNewContact = new javax.swing.JButton();
        jButtonEditContact = new javax.swing.JButton();
        jButtonDeleteContact = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabelGroup = new javax.swing.JLabel();
        jButtonNewGroup = new javax.swing.JButton();
        jButtonEditGroup = new javax.swing.JButton();
        jButtonDeleteGroup = new javax.swing.JButton();
        jSearchField = new javax.swing.JTextField();
        jSearchButton = new javax.swing.JButton();
        jButtonShowNumbers = new javax.swing.JButton();
        jTextFieldError = new javax.swing.JTextField();

        jLabelNewEditGroup.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabelNewEditGroup.setText(translate.getString("ContactsManager.jLabelNewEditGroup.text")); // NOI18N

        jLabelGroupType.setText(translate.getString("ContactsManager.jLabelGroupType.text")); // NOI18N

        jComboBoxGroupType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "FRIENDS", "FAMILY", "OTHERS" }));
        jComboBoxGroupType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxGroupTypeActionPerformed(evt);
            }
        });

        jLabelGroupNote.setText(translate.getString("ContactsManager.jLabelGroupNote.text")); // NOI18N

        jTextFieldGroupNote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldGroupNoteActionPerformed(evt);
            }
        });

        jButtonGroupCancel.setText(translate.getString("ContactsManager.jButtonGroupCancel.text")); // NOI18N
        jButtonGroupCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGroupCancelActionPerformed(evt);
            }
        });

        jButtonGroupOK.setText(translate.getString("ContactsManager.jButtonGroupOK.text")); // NOI18N
        jButtonGroupOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonGroupOKMouseClicked(evt);
            }
        });
        jButtonGroupOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGroupOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialogGroupLayout = new javax.swing.GroupLayout(jDialogGroup.getContentPane());
        jDialogGroup.getContentPane().setLayout(jDialogGroupLayout);
        jDialogGroupLayout.setHorizontalGroup(
            jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogGroupLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonGroupCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonGroupOK)
                .addGap(22, 22, 22))
            .addGroup(jDialogGroupLayout.createSequentialGroup()
                .addGap(107, 107, 107)
                .addGroup(jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialogGroupLayout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabelNewEditGroup))
                    .addComponent(jTextFieldGroupNote, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxGroupType, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(110, Short.MAX_VALUE))
            .addGroup(jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jDialogGroupLayout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addGroup(jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabelGroupType)
                        .addComponent(jLabelGroupNote))
                    .addContainerGap(342, Short.MAX_VALUE)))
        );
        jDialogGroupLayout.setVerticalGroup(
            jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogGroupLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabelNewEditGroup)
                .addGap(18, 18, 18)
                .addComponent(jComboBoxGroupType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jTextFieldGroupNote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                .addGroup(jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonGroupCancel)
                    .addComponent(jButtonGroupOK))
                .addContainerGap())
            .addGroup(jDialogGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jDialogGroupLayout.createSequentialGroup()
                    .addGap(78, 78, 78)
                    .addComponent(jLabelGroupType)
                    .addGap(30, 30, 30)
                    .addComponent(jLabelGroupNote)
                    .addContainerGap(131, Short.MAX_VALUE)))
        );

        jLabelDeleteGroup.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabelDeleteGroup.setText(translate.getString("ContactsManager.jLabelDeleteGroup.text")); // NOI18N

        jLabelDeleteGroupType.setText(translate.getString("ContactsManager.jLabelDeleteGroupType.text")); // NOI18N

        jComboBoxDeleteGroup.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "FRIENDS", "FAMILY", "OTHERS" }));

        jButtonDeleteGroupOK.setText(translate.getString("ContactsManager.jButtonDeleteGroupOK.text")); // NOI18N
        jButtonDeleteGroupOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteGroupOKMouseClicked(evt);
            }
        });
        jButtonDeleteGroupOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteGroupOKActionPerformed(evt);
            }
        });

        jButtonDeleteGroupCancel.setText(translate.getString("ContactsManager.jButtonDeleteGroupCancel.text")); // NOI18N
        jButtonDeleteGroupCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteGroupCancelMouseClicked(evt);
            }
        });
        jButtonDeleteGroupCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteGroupCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialogDeleteGroupLayout = new javax.swing.GroupLayout(jDialogDeleteGroup.getContentPane());
        jDialogDeleteGroup.getContentPane().setLayout(jDialogDeleteGroupLayout);
        jDialogDeleteGroupLayout.setHorizontalGroup(
            jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogDeleteGroupLayout.createSequentialGroup()
                .addGroup(jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialogDeleteGroupLayout.createSequentialGroup()
                        .addGroup(jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jDialogDeleteGroupLayout.createSequentialGroup()
                                .addGap(108, 108, 108)
                                .addComponent(jLabelDeleteGroup))
                            .addGroup(jDialogDeleteGroupLayout.createSequentialGroup()
                                .addGap(42, 42, 42)
                                .addComponent(jLabelDeleteGroupType)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jComboBoxDeleteGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 72, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogDeleteGroupLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonDeleteGroupCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDeleteGroupOK)))
                .addContainerGap())
        );
        jDialogDeleteGroupLayout.setVerticalGroup(
            jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogDeleteGroupLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabelDeleteGroup)
                .addGap(32, 32, 32)
                .addGroup(jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDeleteGroupType)
                    .addComponent(jComboBoxDeleteGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jDialogDeleteGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDeleteGroupOK)
                    .addComponent(jButtonDeleteGroupCancel))
                .addContainerGap())
        );

        jLabelEditContact.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabelEditContact.setText(translate.getString("ContactsManager.jLabelContact.text")); // NOI18N

        jLabelName.setText(translate.getString("ContactsManager.jLabelName.text")); // NOI18N

        jLabelSurname.setText(translate.getString("ContactsManager.jLabelSurname.text")); // NOI18N

        jLabelAddress.setText(translate.getString("ContactsManager.jLabelAddress.text")); // NOI18N

        jTextFieldName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNameActionPerformed(evt);
            }
        });

        jPanelAddToGroup.setBackground(new java.awt.Color(254, 254, 254));
        jPanelAddToGroup.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabelAddToGroup.setText(translate.getString("ContactsManager.jLabelAddToGroup.text")); // NOI18N

        jComboBoxContactGroup.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "FRIENDS", "FAMILY", "OTHERS" }));

        javax.swing.GroupLayout jPanelAddToGroupLayout = new javax.swing.GroupLayout(jPanelAddToGroup);
        jPanelAddToGroup.setLayout(jPanelAddToGroupLayout);
        jPanelAddToGroupLayout.setHorizontalGroup(
            jPanelAddToGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAddToGroupLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanelAddToGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelAddToGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBoxContactGroup, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanelAddToGroupLayout.setVerticalGroup(
            jPanelAddToGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAddToGroupLayout.createSequentialGroup()
                .addComponent(jLabelAddToGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxContactGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 64, Short.MAX_VALUE))
        );

        jLabelNumbers.setText(translate.getString("ContactsManager.jLabelNumbers")); // NOI18N

        jPanelNumbers.setBackground(new java.awt.Color(254, 254, 254));
        jPanelNumbers.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jComboBoxNumbers1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "HOME", "MOBILE", "FAX" }));
        jComboBoxNumbers1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxNumbers1ActionPerformed(evt);
            }
        });

        jComboBoxNumbers2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "HOME", "MOBILE", "FAX" }));

        jComboBoxNumbers3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "HOME", "MOBILE", "FAX" }));

        jComboBoxNumbers4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "HOME", "MOBILE", "FAX" }));

        jComboBoxNumbers5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "WORK", "HOME", "MOBILE", "FAX" }));

        javax.swing.GroupLayout jPanelNumbersLayout = new javax.swing.GroupLayout(jPanelNumbers);
        jPanelNumbers.setLayout(jPanelNumbersLayout);
        jPanelNumbersLayout.setHorizontalGroup(
            jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNumbersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBoxNumbers5, 0, 110, Short.MAX_VALUE)
                    .addComponent(jComboBoxNumbers4, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBoxNumbers3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBoxNumbers2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBoxNumbers1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldNumbers1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanelNumbersLayout.setVerticalGroup(
            jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNumbersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxNumbers1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxNumbers2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxNumbers3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxNumbers4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNumbers4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelNumbersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxNumbers5)
                    .addComponent(jTextFieldNumbers5, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jButtonContactOK.setText(translate.getString("ContactsManager.jButtonContactOK")); // NOI18N
        jButtonContactOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonContactOKMouseClicked(evt);
            }
        });
        jButtonContactOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonContactOKActionPerformed(evt);
            }
        });

        jButtonContactCancel.setText(translate.getString("ContactsManager.jButtonContactCancel")); // NOI18N
        jButtonContactCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonContactCancelMouseClicked(evt);
            }
        });
        jButtonContactCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonContactCancelActionPerformed(evt);
            }
        });

        textContactID.setEditable(false);

        javax.swing.GroupLayout jDialogContactLayout = new javax.swing.GroupLayout(jDialogContact.getContentPane());
        jDialogContact.getContentPane().setLayout(jDialogContactLayout);
        jDialogContactLayout.setHorizontalGroup(
            jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogContactLayout.createSequentialGroup()
                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jDialogContactLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonContactCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonContactOK))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jDialogContactLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jDialogContactLayout.createSequentialGroup()
                                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogContactLayout.createSequentialGroup()
                                        .addComponent(jLabelSurname)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTextFieldSurname, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogContactLayout.createSequentialGroup()
                                        .addComponent(jLabelName)
                                        .addGap(62, 62, 62)
                                        .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(59, 59, 59)
                                .addComponent(jPanelAddToGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jDialogContactLayout.createSequentialGroup()
                                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelAddress)
                                    .addComponent(jLabelNumbers))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jDialogContactLayout.createSequentialGroup()
                                        .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(190, 190, 190))
                                    .addComponent(jPanelNumbers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addGap(40, 40, 40))
            .addGroup(jDialogContactLayout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(textContactID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(73, 73, 73)
                .addComponent(jLabelEditContact)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jDialogContactLayout.setVerticalGroup(
            jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogContactLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelEditContact)
                    .addComponent(textContactID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jDialogContactLayout.createSequentialGroup()
                        .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelSurname)
                            .addComponent(jTextFieldSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelAddress)
                            .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanelAddToGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(21, 21, 21)
                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNumbers)
                    .addComponent(jPanelNumbers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(jDialogContactLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonContactOK)
                    .addComponent(jButtonContactCancel))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText(translate.getString("ContactsManager.jLabel1.text")); // NOI18N

        jShowGroup.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALL CONTACTS", "WORK", "FRIENDS", "FAMILY", "OTHERS" }));
        jShowGroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jShowGroupMouseClicked(evt);
            }
        });
        jShowGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jShowGroupActionPerformed(evt);
            }
        });

        jNote.setEditable(false);
        jNote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNoteActionPerformed(evt);
            }
        });

        jTableContact.setModel(new ContactTableModel());
        jScrollPane1.setViewportView(jTableContact);

        jTableNumber.setModel(new NumberTableModel());
        jScrollPane2.setViewportView(jTableNumber);

        jPanel1.setBackground(new java.awt.Color(207, 204, 201));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabelContact.setText(translate.getString("ContactsManager.jLabelContact.text")); // NOI18N

        jButtonNewContact.setText(translate.getString("ContactsManager.jButtonNewContact.text")); // NOI18N
        jButtonNewContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNewContactMouseClicked(evt);
            }
        });
        jButtonNewContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewContactActionPerformed(evt);
            }
        });

        jButtonEditContact.setText(translate.getString("ContactsManager.jButtonEditContact.text")); // NOI18N
        jButtonEditContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonEditContactMouseClicked(evt);
            }
        });
        jButtonEditContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditContactActionPerformed(evt);
            }
        });

        jButtonDeleteContact.setText(translate.getString("ContactsManager.jButtonDeleteContact.text")); // NOI18N
        jButtonDeleteContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteContactMouseClicked(evt);
            }
        });
        jButtonDeleteContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteContactActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButtonNewContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonEditContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonDeleteContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelContact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNewContact, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEditContact, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDeleteContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(207, 204, 201));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabelGroup.setText(translate.getString("ContactsManager.jLabelGroup.text")); // NOI18N

        jButtonNewGroup.setText(translate.getString("ContactsManager.jButtonNewGroup.text")); // NOI18N
        jButtonNewGroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNewGroupMouseClicked(evt);
            }
        });
        jButtonNewGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewGroupActionPerformed(evt);
            }
        });

        jButtonEditGroup.setText(translate.getString("ContactsManager.jButtonEditGroup.text")); // NOI18N
        jButtonEditGroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonEditGroupMouseClicked(evt);
            }
        });
        jButtonEditGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditGroupActionPerformed(evt);
            }
        });

        jButtonDeleteGroup.setText(translate.getString("ContactsManager.jButtonDeleteGroup.text")); // NOI18N
        jButtonDeleteGroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteGroupMouseClicked(evt);
            }
        });
        jButtonDeleteGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteGroupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(29, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButtonNewGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonEditGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonDeleteGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNewGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEditGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDeleteGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSearchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchFieldActionPerformed(evt);
            }
        });

        jSearchButton.setText(translate.getString("ContactsManager.jSearchButton.text")); // NOI18N
        jSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchButtonActionPerformed(evt);
            }
        });

        jButtonShowNumbers.setText(translate.getString("ContactsManager.jButtonShowNumbers.text")); // NOI18N
        jButtonShowNumbers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonShowNumbersActionPerformed(evt);
            }
        });

        jTextFieldError.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(261, 261, 261)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jShowGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 277, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonShowNumbers)
                .addGap(254, 254, 254))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextFieldError, javax.swing.GroupLayout.PREFERRED_SIZE, 595, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSearchButton)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jNote)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jShowGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jNote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSearchButton))
                .addGap(12, 12, 12)
                .addComponent(jButtonShowNumbers)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jTextFieldError, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jNoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNoteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jNoteActionPerformed

    private void jButtonNewGroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonNewGroupMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonNewGroupMouseClicked

    private void jButtonEditGroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonEditGroupMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonEditGroupMouseClicked

    private void jButtonDeleteGroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonDeleteGroupMouseClicked

    private void jButtonNewContactMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonNewContactMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonNewContactMouseClicked

    private void jButtonEditContactMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonEditContactMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonEditContactMouseClicked

    private void jButtonDeleteContactMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteContactMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonDeleteContactMouseClicked

    private void jShowGroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jShowGroupMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jShowGroupMouseClicked

    private void jTextFieldGroupNoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldGroupNoteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldGroupNoteActionPerformed

    private void jButtonGroupOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonGroupOKMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonGroupOKMouseClicked

    private void jButtonDeleteGroupOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupOKMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonDeleteGroupOKMouseClicked

    private void jButtonDeleteGroupCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupCancelMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonDeleteGroupCancelMouseClicked

    private void jButtonContactOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonContactOKMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonContactOKMouseClicked

    private void jButtonContactCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonContactCancelMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonContactCancelMouseClicked

    private void jSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchButtonActionPerformed
        jTextFieldError.setText("");
        String search = jSearchField.getText();
        Contact contact = null;
        if (search != null && !"".equals(search)) 
        {
            try
            {
                log.log(Level.INFO, "searching...");
                contact = contactManager.findContactByName(search);
            } catch (IndexOutOfBoundsException e)
            {
                log.log(Level.INFO, "error searching");
                jTextFieldError.setText("you must enter whole name to search window");
            }
        }
        else
        {
            numberTableModel.clear();
            contactTableModel.clear();
            contactsSwingWorker = new ContactsSwingWorker();
            contactsSwingWorker.execute();
        }
        numberTableModel.clear();
        contactTableModel.clear();
        if (contact == null)
        {
            return;
        }
        else
        {
            contactTableModel.addContact(contact);
        }
    }//GEN-LAST:event_jSearchButtonActionPerformed

    private void jButtonNewContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewContactActionPerformed
        jTextFieldError.setText("");
        textContactID.setText("");
        jTextFieldName.setText("");
        jTextFieldSurname.setText("");
        jTextFieldAddress.setText("");
        jComboBoxNumbers1.setSelectedIndex(0);
        jTextFieldNumbers1.setText("");
        jComboBoxNumbers2.setSelectedIndex(0);
        jTextFieldNumbers2.setText("");
        jComboBoxNumbers3.setSelectedIndex(0);
        jTextFieldNumbers3.setText("");
        jComboBoxNumbers4.setSelectedIndex(0);
        jTextFieldNumbers4.setText("");
        jComboBoxNumbers5.setSelectedIndex(0);
        jTextFieldNumbers5.setText("");
        jComboBoxContactGroup.setSelectedIndex(0);
        jDialogContact.pack();
        jDialogContact.setLocationRelativeTo(null);
        jDialogContact.setVisible(true);
    }//GEN-LAST:event_jButtonNewContactActionPerformed

    private void jButtonNewGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewGroupActionPerformed
        jTextFieldError.setText("");
        jComboBoxGroupType.setSelectedIndex(0);
        jTextFieldGroupNote.setText("");
        jDialogGroup.pack();
        jDialogGroup.setLocationRelativeTo(null);
        jDialogGroup.setVisible(true);
    }//GEN-LAST:event_jButtonNewGroupActionPerformed

    private void jTextFieldNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldNameActionPerformed

    private void jButtonContactCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonContactCancelActionPerformed
        jTextFieldError.setText("");
        jDialogContact.setVisible(false);
    }//GEN-LAST:event_jButtonContactCancelActionPerformed

    private void jButtonDeleteGroupCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupCancelActionPerformed
        jTextFieldError.setText("");
        jDialogDeleteGroup.setVisible(false);
    }//GEN-LAST:event_jButtonDeleteGroupCancelActionPerformed

    private void jButtonGroupCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGroupCancelActionPerformed
        jTextFieldError.setText("");
        jDialogGroup.setVisible(false);
    }//GEN-LAST:event_jButtonGroupCancelActionPerformed

    private void jComboBoxGroupTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxGroupTypeActionPerformed
        jTextFieldError.setText("");
        Group group = null;
        try
        {
            switch(jComboBoxGroupType.getSelectedIndex())
            {
                case 0:
                    group = groupManager.findGroupByType(GroupType.WORK);
                    break;
                case 1:
                    group = groupManager.findGroupByType(GroupType.FRIENDS);
                    break;
                case 2:
                    group = groupManager.findGroupByType(GroupType.FAMILY);
                    break;
                case 3:
                    group = groupManager.findGroupByType(GroupType.OTHERS);
                    break;
            }
        } catch (AppException e)
        {
            log.log(Level.SEVERE, "Application Error", e);
        }
        if (group == null)
        {
            jTextFieldGroupNote.setText("");
        }
        else
        {
            if (group.getNote() == null)
            {
                jTextFieldGroupNote.setText("");
            }
            else
            {
                jTextFieldGroupNote.setText(group.getNote());
            }
        }
    }//GEN-LAST:event_jComboBoxGroupTypeActionPerformed

    private void jButtonDeleteGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupActionPerformed
        jTextFieldError.setText("");
        jDialogDeleteGroup.pack();
        jDialogDeleteGroup.setLocationRelativeTo(null);
        jDialogDeleteGroup.setVisible(true);
    }//GEN-LAST:event_jButtonDeleteGroupActionPerformed

    private void jSearchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jSearchFieldActionPerformed

    private void jShowGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jShowGroupActionPerformed
        jTextFieldError.setText("");
        int action = jShowGroup.getSelectedIndex();
        numberTableModel.clear();
        contactTableModel.clear();
        jNote.setText("");
        Group group = null;
        switch(action)
        {
            case 0:
                log.log(Level.INFO, "listing all contacts");
                contactsSwingWorker = new ContactsSwingWorker();
                contactsSwingWorker.execute();
                break;
            case 1:
                log.log(Level.INFO, "listing work contacts");
                try {
                    if ((group = groupManager.findGroupByType(GroupType.WORK)) == null)
                        return;
                    jNote.setText(group.getNote());
                } catch (AppException ex) {
                    log.log(Level.SEVERE, "group not in db", ex);
                    contactsSwingWorker = new ContactsSwingWorker();
                    contactsSwingWorker.execute();
                    return;
                }
                for(Contact contact : contactsManager.findAllContactsInGroup(group))
                {
                    contactTableModel.addContact(contact);
                }
                break;
            case 2:
                log.log(Level.INFO, "listing friends contacts");
                try {
                    if ((group = groupManager.findGroupByType(GroupType.FRIENDS)) == null)
                        return;
                    jNote.setText(group.getNote());
                } catch (AppException ex) {
                    log.log(Level.SEVERE, "group not in db", ex);
                    contactsSwingWorker = new ContactsSwingWorker();
                    contactsSwingWorker.execute();
                    return;
                }
                for(Contact contact : contactsManager.findAllContactsInGroup(group))
                {
                    contactTableModel.addContact(contact);
                }
                break;
            case 3:
                log.log(Level.INFO, "listing family contacts");
                try {
                    if ((group = groupManager.findGroupByType(GroupType.FAMILY)) == null)
                        return;
                    jNote.setText(group.getNote());
                } catch (AppException ex) {
                    log.log(Level.SEVERE, "group not in db", ex);
                    contactsSwingWorker = new ContactsSwingWorker();
                    contactsSwingWorker.execute();
                    return;
                }
                for(Contact contact : contactsManager.findAllContactsInGroup(group))
                {
                    contactTableModel.addContact(contact);
                }
                break;
            case 4:
                log.log(Level.INFO, "listing other contacts");
                try {
                    if ((group = groupManager.findGroupByType(GroupType.OTHERS)) == null)
                        return;
                    jNote.setText(group.getNote());
                } catch (AppException ex) {
                    log.log(Level.SEVERE, "group not in db", ex);
                    contactsSwingWorker = new ContactsSwingWorker();
                    contactsSwingWorker.execute();
                    return;
                }
                for(Contact contact : contactsManager.findAllContactsInGroup(group))
                {
                    contactTableModel.addContact(contact);
                }
                break;
        }
    }//GEN-LAST:event_jShowGroupActionPerformed

    private void jButtonEditContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditContactActionPerformed
        jTextFieldError.setText("");
        if (jTableContact.getSelectedRowCount() != 1)
        {
            jTextFieldError.setText("select 1 contact");
            return;
        }
        Long contact_id = (Long) contactTableModel.getValueAt(jTableContact.getSelectedRow(), 0);
        Contact contact = contactManager.findContactByID(contact_id);
        if (contact == null)
        {
            log.log(Level.WARNING, "could not find contact in db.");
            return;
        }
        textContactID.setText(String.valueOf(contact.getId()));
        
        jTextFieldName.setText(contact.getName());
        jTextFieldSurname.setText(contact.getSurname());
        jTextFieldAddress.setText(contact.getAddress());
        
        Map<String, NumberType> numbers = contact.getPhoneNumbers();
        int counter = 0;
        for (Map.Entry<String, NumberType> entry : numbers.entrySet())
        {
            if (counter == 0)
            {
                switch(entry.getValue().toString())
                {
                    case "WORK":
                        jComboBoxNumbers1.setSelectedIndex(0);
                        jTextFieldNumbers1.setText(entry.getKey());
                        break;
                    case "HOME":
                        jComboBoxNumbers1.setSelectedIndex(1);
                        jTextFieldNumbers1.setText(entry.getKey());
                        break;
                    case "MOBILE":
                        jComboBoxNumbers1.setSelectedIndex(2);
                        jTextFieldNumbers1.setText(entry.getKey());
                        break;
                    case "FAX":
                        jComboBoxNumbers1.setSelectedIndex(3);
                        jTextFieldNumbers1.setText(entry.getKey());
                        break;
                }
            }
            if (counter == 1)
            {
                switch(entry.getValue().toString())
                {
                    case "WORK":
                        jComboBoxNumbers2.setSelectedIndex(0);
                        jTextFieldNumbers2.setText(entry.getKey());
                        break;
                    case "HOME":
                        jComboBoxNumbers2.setSelectedIndex(1);
                        jTextFieldNumbers2.setText(entry.getKey());
                        break;
                    case "MOBILE":
                        jComboBoxNumbers2.setSelectedIndex(2);
                        jTextFieldNumbers2.setText(entry.getKey());
                        break;
                    case "FAX":
                        jComboBoxNumbers2.setSelectedIndex(3);
                        jTextFieldNumbers2.setText(entry.getKey());
                        break;
                }
            }
            if (counter == 2)
            {
                switch(entry.getValue().toString())
                {
                    case "WORK":
                        jComboBoxNumbers3.setSelectedIndex(0);
                        jTextFieldNumbers3.setText(entry.getKey());
                        break;
                    case "HOME":
                        jComboBoxNumbers3.setSelectedIndex(1);
                        jTextFieldNumbers3.setText(entry.getKey());
                        break;
                    case "MOBILE":
                        jComboBoxNumbers3.setSelectedIndex(2);
                        jTextFieldNumbers3.setText(entry.getKey());
                        break;
                    case "FAX":
                        jComboBoxNumbers3.setSelectedIndex(3);
                        jTextFieldNumbers3.setText(entry.getKey());
                        break;
                }
            }
            if (counter == 3)
            {
                switch(entry.getValue().toString())
                {
                    case "WORK":
                        jComboBoxNumbers4.setSelectedIndex(0);
                        jTextFieldNumbers4.setText(entry.getKey());
                        break;
                    case "HOME":
                        jComboBoxNumbers4.setSelectedIndex(1);
                        jTextFieldNumbers4.setText(entry.getKey());
                        break;
                    case "MOBILE":
                        jComboBoxNumbers4.setSelectedIndex(2);
                        jTextFieldNumbers4.setText(entry.getKey());
                        break;
                    case "FAX":
                        jComboBoxNumbers4.setSelectedIndex(3);
                        jTextFieldNumbers4.setText(entry.getKey());
                        break;
                }
            }
            if (counter == 4)
            {
                switch(entry.getValue().toString())
                {
                    case "WORK":
                        jComboBoxNumbers5.setSelectedIndex(0);
                        jTextFieldNumbers5.setText(entry.getKey());
                        break;
                    case "HOME":
                        jComboBoxNumbers5.setSelectedIndex(1);
                        jTextFieldNumbers5.setText(entry.getKey());
                        break;
                    case "MOBILE":
                        jComboBoxNumbers5.setSelectedIndex(2);
                        jTextFieldNumbers5.setText(entry.getKey());
                        break;
                    case "FAX":
                        jComboBoxNumbers5.setSelectedIndex(3);
                        jTextFieldNumbers5.setText(entry.getKey());
                        break;
                }
            }
            counter++;
        }
        
        switch(counter)
        {
            case 0:
                jComboBoxNumbers1.setSelectedIndex(0);
                jTextFieldNumbers1.setText("");
            case 1:
                jComboBoxNumbers2.setSelectedIndex(0);
                jTextFieldNumbers2.setText("");
            case 2:
                jComboBoxNumbers3.setSelectedIndex(0);
                jTextFieldNumbers3.setText("");
            case 3:
                jComboBoxNumbers4.setSelectedIndex(0);
                jTextFieldNumbers4.setText("");
            case 4:
                jComboBoxNumbers5.setSelectedIndex(0);
                jTextFieldNumbers5.setText("");
        }
        
        switch(contactsManager.findGroupWithContact(contact).getType().toString())
        {
            case "WORK":
                jComboBoxContactGroup.setSelectedIndex(0);
                break;
            case "FRIENDS":
                jComboBoxContactGroup.setSelectedIndex(1);
                break;
            case "FAMILY":
                jComboBoxContactGroup.setSelectedIndex(2);
                break;
            case "OTHERS":
                jComboBoxContactGroup.setSelectedIndex(3);
                break;
        }
        
        jDialogContact.pack();
        jDialogContact.setLocationRelativeTo(null);
        jDialogContact.setVisible(true);
    }//GEN-LAST:event_jButtonEditContactActionPerformed

    private void jComboBoxNumbers1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxNumbers1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxNumbers1ActionPerformed

    private void jButtonContactOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonContactOKActionPerformed
        jTextFieldError.setText("");
        Contact contact = new Contact();
        
        contact.setName(jTextFieldName.getText());
        contact.setSurname(jTextFieldSurname.getText());
        contact.setAddress(jTextFieldAddress.getText());
        Map<String, NumberType> numbers = new TreeMap<String, NumberType>();
        if (!"".equals(jTextFieldNumbers1.getText()))
            numbers.put(jTextFieldNumbers1.getText(), Enum.valueOf(NumberType.class, jComboBoxNumbers1.getSelectedItem().toString()));
        if (!"".equals(jTextFieldNumbers2.getText()))
            numbers.put(jTextFieldNumbers2.getText(), Enum.valueOf(NumberType.class, jComboBoxNumbers2.getSelectedItem().toString()));
        if (!"".equals(jTextFieldNumbers3.getText()))
            numbers.put(jTextFieldNumbers3.getText(), Enum.valueOf(NumberType.class, jComboBoxNumbers3.getSelectedItem().toString()));
        if (!"".equals(jTextFieldNumbers4.getText()))
            numbers.put(jTextFieldNumbers4.getText(), Enum.valueOf(NumberType.class, jComboBoxNumbers4.getSelectedItem().toString()));
        if (!"".equals(jTextFieldNumbers5.getText()))
            numbers.put(jTextFieldNumbers5.getText(), Enum.valueOf(NumberType.class, jComboBoxNumbers5.getSelectedItem().toString()));
        contact.setPhoneNumbers(numbers);
        
        try
        {
            if (textContactID.getText().equals(""))  // Add
            {
                log.log(Level.INFO, "Adding contact");
                contactManager.newContact(contact);
                Group group;
                boolean isGroup = false;
                try
                {
                switch(jComboBoxContactGroup.getSelectedIndex())
                {
                    case 0:
                        if ((group = groupManager.findGroupByType(GroupType.WORK)) != null)
                        {
                            contactsManager.addContactToGroup(contact, group);
                            isGroup = true;
                        }
                        break;
                    case 1:
                        if ((group = groupManager.findGroupByType(GroupType.FRIENDS)) != null)
                        {
                            contactsManager.addContactToGroup(contact, group);
                            isGroup = true;
                        }
                        break;
                    case 2:
                        if ((group = groupManager.findGroupByType(GroupType.FAMILY)) != null)
                        {
                            contactsManager.addContactToGroup(contact, group);
                            isGroup = true;
                        }
                        break;
                    case 3:
                        if ((group = groupManager.findGroupByType(GroupType.OTHERS)) != null)
                        {
                            contactsManager.addContactToGroup(contact, group);
                            isGroup = true;
                        }
                        break;
                }
                } catch (AppException e)
                {
                    log.log(Level.SEVERE, "Application error", e);
                }
                if (!isGroup)
                {
                    jTextFieldError.setText("Group does not exist");
                    contactManager.deleteContact(contact);
                }
                else
                {
                    contactTableModel.addContact(contact);
                }
            }
            else  //update
            {
                log.log(Level.INFO, "Editing contact");
                Long contactID = Long.valueOf(textContactID.getText());
                contact.setId(contactID);
                Contact contactCached = contactManager.findContactByID(contactID);
                boolean isGroup = false;
                if (!contactsManager.findGroupWithContact(contact).getType().toString().equals(jComboBoxContactGroup.getSelectedItem().toString()))
                {
                    contactsManager.removeContactFromGroup(contact, contactsManager.findGroupWithContact(contact));
                    Group group;
                    try
                    {
                    switch(jComboBoxContactGroup.getSelectedIndex())
                    {
                        case 0:
                            log.log(Level.INFO, "WORK");
                            if ((group = groupManager.findGroupByType(GroupType.WORK)) != null)
                            {
                                log.log(Level.INFO, "group found");
                                contactsManager.addContactToGroup(contact, group);
                                isGroup = true;
                            }
                        break;
                        case 1:
                            if ((group = groupManager.findGroupByType(GroupType.FRIENDS)) != null)
                            {
                                contactsManager.addContactToGroup(contact, group);
                                isGroup = true;
                            }
                        case 2:
                            if ((group = groupManager.findGroupByType(GroupType.FAMILY)) != null)
                            {
                                contactsManager.addContactToGroup(contact, group);
                                isGroup = true;
                            }
                        case 3:
                            if ((group = groupManager.findGroupByType(GroupType.OTHERS)) != null)
                            {
                                contactsManager.addContactToGroup(contact, group);
                                isGroup = true;
                            }
                    }
                    } catch (AppException e)
                    {
                        log.log(Level.SEVERE, "Application error", e);
                    }
                    
                }
                else
                {
                    isGroup = true;
                }
                
                if (isGroup)
                {
                    contactManager.editContact(contact);
                    contactTableModel.removeContact(contactCached);
                    contactTableModel.addContact(contact);
                }
                else
                {
                    jTextFieldError.setText("Group does not exist");
                    contactTableModel.removeContact(contact);
                    contactManager.deleteContact(contact);
                }
            }
        } catch (Exception e)
        {
            log.log(Level.INFO, "User request failed");
        }
        
        jDialogContact.setVisible(false);
    }//GEN-LAST:event_jButtonContactOKActionPerformed

    private void jButtonDeleteContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteContactActionPerformed
        jTextFieldError.setText("");
        if (jTableContact.getSelectedRowCount() == 0)
        {
            return;
        }
        int[] rows = jTableContact.getSelectedRows();
        for (int i = 0; i < rows.length; i++)
        {
            Contact contact = contactManager.findContactByID(Long.valueOf(jTableContact.getValueAt(rows[i], 0).toString()));
            contactsManager.removeContactFromGroup(contact, contactsManager.findGroupWithContact(contact));
            contactManager.deleteContact(contact);
            contactTableModel.removeContact(contact);
        }
    }//GEN-LAST:event_jButtonDeleteContactActionPerformed

    private void jButtonEditGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditGroupActionPerformed
        jTextFieldError.setText("");
        jComboBoxGroupType.setSelectedIndex(0);
        try
        {
            jTextFieldGroupNote.setText(groupManager.findGroupByType(GroupType.WORK).getNote());
        } catch (AppException e)
        {
            log.log(Level.SEVERE, "Application Error");
        }
        jDialogGroup.pack();
        jDialogGroup.setLocationRelativeTo(null);
        jDialogGroup.setVisible(true);
    }//GEN-LAST:event_jButtonEditGroupActionPerformed

    private void jButtonGroupOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGroupOKActionPerformed
        jTextFieldError.setText("");
        Group group = null;
        int type = jComboBoxGroupType.getSelectedIndex();
        try
        {
            switch(type)
            {
                case 0:
                    if ((group = groupManager.findGroupByType(GroupType.WORK)) == null) //add
                    {
                        group = new Group();
                        group.setType(GroupType.WORK);
//                        if (!"".equals(jTextFieldGroupNote.getText()))
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.newGroup(group);
                    }
                    else  //edit
                    {
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.editGroup(group);
                    }
                    break;
                case 1:
                    if ((group = groupManager.findGroupByType(GroupType.FRIENDS)) == null) //add
                    {
                        group = new Group();
                        group.setType(GroupType.FRIENDS);
//                        if (!"".equals(jTextFieldGroupNote.getText()))
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.newGroup(group);
                    }
                    else  //edit
                    {
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.editGroup(group);
                    }
                    break;
                case 2:
                    if ((group = groupManager.findGroupByType(GroupType.FAMILY)) == null) //add
                    {
                        group = new Group();
                        group.setType(GroupType.FAMILY);
//                        if (!"".equals(jTextFieldGroupNote.getText()))
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.newGroup(group);
                    }
                    else  //edit
                    {
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.editGroup(group);
                    }
                    break;
                case 3:
                    if ((group = groupManager.findGroupByType(GroupType.OTHERS)) == null) //add
                    {
                        group = new Group();
                        group.setType(GroupType.OTHERS);
//                        if (!"".equals(jTextFieldGroupNote.getText()))
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.newGroup(group);
                    }
                    else  //edit
                    {
                        group.setNote(jTextFieldGroupNote.getText());
                        groupManager.editGroup(group);
                    }
                    break;
            }
        } catch (AppException e)
        {
            log.log(Level.SEVERE, "Application Error", e);
        }
        jDialogGroup.setVisible(false);
    }//GEN-LAST:event_jButtonGroupOKActionPerformed

    private void jButtonDeleteGroupOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteGroupOKActionPerformed
        jTextFieldError.setText("");
        int type = jComboBoxDeleteGroup.getSelectedIndex();
        Group group = null;
        List<Contact> contacts;
        try
        {
            switch(type)
            {
                case 0:
                    if ((group = groupManager.findGroupByType(GroupType.WORK)) == null)
                        break;
                    contacts = contactsManager.findAllContactsInGroup(group);
                    for (Contact contact : contacts)
                    {
                        contactsManager.removeContactFromGroup(contact, group);
                        contactManager.deleteContact(contact);
                    }
                    groupManager.deleteGroup(group);
                    break;
                case 1:
                    if ((group = groupManager.findGroupByType(GroupType.FRIENDS)) == null)
                        break;
                    contacts = contactsManager.findAllContactsInGroup(group);
                    for (Contact contact : contacts)
                    {
                        contactsManager.removeContactFromGroup(contact, group);
                        contactManager.deleteContact(contact);
                    }
                    groupManager.deleteGroup(group);
                    break;
                case 2:
                    if ((group = groupManager.findGroupByType(GroupType.FAMILY)) == null)
                        break;
                    contacts = contactsManager.findAllContactsInGroup(group);
                    for (Contact contact : contacts)
                    {
                        contactsManager.removeContactFromGroup(contact, group);
                        contactManager.deleteContact(contact);
                    }
                    groupManager.deleteGroup(group);
                    break;
                case 3:
                    if ((group = groupManager.findGroupByType(GroupType.OTHERS)) == null)
                        break;
                    contacts = contactsManager.findAllContactsInGroup(group);
                    for (Contact contact : contacts)
                    {
                        contactsManager.removeContactFromGroup(contact, group);
                        contactManager.deleteContact(contact);
                    }
                    groupManager.deleteGroup(group);
                    break;
            }
        } catch (AppException e)
        {
            log.log(Level.SEVERE, "App Error", e);
        }
        jDialogDeleteGroup.setVisible(false);
        contactsSwingWorker = new ContactsSwingWorker();
        contactsSwingWorker.execute();
    }//GEN-LAST:event_jButtonDeleteGroupOKActionPerformed

    private void jButtonShowNumbersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonShowNumbersActionPerformed
        jTextFieldError.setText("");
        numberTableModel.clear();
        if (jTableContact.getSelectedRowCount() != 1)
        {
            jTextFieldError.setText("please, select contact");
            return;
        }
        Long contact_id = (Long) contactTableModel.getValueAt(jTableContact.getSelectedRow(), 0);
        Contact contact = contactManager.findContactByID(contact_id);
        if (contact == null)
        {
            log.log(Level.WARNING, "could not find contact in db.");
            return;
        }
        
        Map<String, NumberType> numbers = contact.getPhoneNumbers();
        
        for (Map.Entry<String, NumberType> entry : numbers.entrySet())
        {
            numberTableModel.addNumber(entry.getValue().toString(), entry.getKey());
        }
    }//GEN-LAST:event_jButtonShowNumbersActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ContactsManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ContactsManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ContactsManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ContactsManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ContactsManager().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonContactCancel;
    private javax.swing.JButton jButtonContactOK;
    private javax.swing.JButton jButtonDeleteContact;
    private javax.swing.JButton jButtonDeleteGroup;
    private javax.swing.JButton jButtonDeleteGroupCancel;
    private javax.swing.JButton jButtonDeleteGroupOK;
    private javax.swing.JButton jButtonEditContact;
    private javax.swing.JButton jButtonEditGroup;
    private javax.swing.JButton jButtonGroupCancel;
    private javax.swing.JButton jButtonGroupOK;
    private javax.swing.JButton jButtonNewContact;
    private javax.swing.JButton jButtonNewGroup;
    private javax.swing.JButton jButtonShowNumbers;
    private javax.swing.JComboBox jComboBoxContactGroup;
    private javax.swing.JComboBox jComboBoxDeleteGroup;
    private javax.swing.JComboBox jComboBoxGroupType;
    private javax.swing.JComboBox jComboBoxNumbers1;
    private javax.swing.JComboBox jComboBoxNumbers2;
    private javax.swing.JComboBox jComboBoxNumbers3;
    private javax.swing.JComboBox jComboBoxNumbers4;
    private javax.swing.JComboBox jComboBoxNumbers5;
    private javax.swing.JDialog jDialogContact;
    private javax.swing.JDialog jDialogDeleteGroup;
    private javax.swing.JDialog jDialogGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelAddToGroup;
    private javax.swing.JLabel jLabelAddress;
    private javax.swing.JLabel jLabelContact;
    private javax.swing.JLabel jLabelDeleteGroup;
    private javax.swing.JLabel jLabelDeleteGroupType;
    private javax.swing.JLabel jLabelEditContact;
    private javax.swing.JLabel jLabelGroup;
    private javax.swing.JLabel jLabelGroupNote;
    private javax.swing.JLabel jLabelGroupType;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelNewEditGroup;
    private javax.swing.JLabel jLabelNumbers;
    private javax.swing.JLabel jLabelSurname;
    private javax.swing.JTextField jNote;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelAddToGroup;
    private javax.swing.JPanel jPanelNumbers;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jSearchButton;
    private javax.swing.JTextField jSearchField;
    private javax.swing.JComboBox jShowGroup;
    private javax.swing.JTable jTableContact;
    private javax.swing.JTable jTableNumber;
    private javax.swing.JTextField jTextFieldAddress;
    private javax.swing.JTextField jTextFieldError;
    private javax.swing.JTextField jTextFieldGroupNote;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldNumbers1;
    private javax.swing.JTextField jTextFieldNumbers2;
    private javax.swing.JTextField jTextFieldNumbers3;
    private javax.swing.JTextField jTextFieldNumbers4;
    private javax.swing.JTextField jTextFieldNumbers5;
    private javax.swing.JTextField jTextFieldSurname;
    private javax.swing.JTextField textContactID;
    // End of variables declaration//GEN-END:variables
}
